package com.laoniu.biz.base;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.laoniu.dto.PageResponse;
import com.laoniu.dto.RestResult;
import com.laoniu.dto.request.BaseGoodsRequestDto;
import com.laoniu.dto.response.BaseGoodsDto;
import com.laoniu.entity.BaseGoods;
import com.laoniu.entity.SecurityUser;
import com.laoniu.service.base.BaseGoodsService;
import com.laoniu.utils.SpringSecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class BaseGoodsBiz {

    @Autowired
    private BaseGoodsService goodsService;

    /**
     * 分页查询商品
     * @param requestDto
     * @return
     */
    public PageResponse<BaseGoodsDto> listPage(BaseGoodsRequestDto requestDto){

        Page<BaseGoods> page = goodsService.listPage(requestDto);
        PageResponse<BaseGoodsDto> result = new PageResponse<BaseGoodsDto>();
        result.setPageNo(requestDto.getPageNo());
        result.setPageSize(requestDto.getPageSize());
        result.setTotal(page.getTotal());
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            List<BaseGoodsDto> dtos = Lists.newArrayList();
            for (BaseGoods record : page.getRecords()) {
                BaseGoodsDto dto = Convert.convert(BaseGoodsDto.class, record);
                dtos.add(dto);
            }
            result.setData(dtos);
        }
        return result;
    }

    public void addGoods(BaseGoodsDto dto){
        SecurityUser user = SpringSecurityUtils.getUser();
        BaseGoods goods = Convert.convert(BaseGoods.class, dto);
        goods.setCreateTime(LocalDateTime.now());
        goods.setUpdateTime(LocalDateTime.now());
        goods.setOpId(user.getId());
        goods.setOpName(user.getName());
        goodsService.save(goods);
    }

    public void updateGoods(BaseGoodsDto dto){
        SecurityUser user = SpringSecurityUtils.getUser();
        if (Objects.isNull(dto.getId())){
            return;
        }
        BaseGoods goods = goodsService.getById(dto.getId());
        if (Objects.isNull(goods)){
            return;
        }
        goods.setName(dto.getName());
        goods.setBaseSupplier(dto.getBaseSupplier());
        goods.setAddress(dto.getAddress());
        goods.setDescription(dto.getDescription());
        goods.setPrice(dto.getPrice());
        goods.setNum(dto.getNum());
        goods.setPic(dto.getPic());
        goods.setStandard(dto.getStandard());
        goods.setPackCompany(dto.getPackCompany());
        goods.setApprovalNumber(dto.getApprovalNumber());
        goods.setStatus(dto.getStatus());
        goods.setUpdateTime(LocalDateTime.now());
        goods.setOpId(user.getId());
        goods.setOpName(user.getName());
        goodsService.updateById(goods);
    }

    public RestResult delete(Long id){
        if (Objects.isNull(id)){
            return RestResult.fail("id不能为空");
        }
        BaseGoods goods = goodsService.getById(id);
        if (Objects.isNull(goods)){
            return RestResult.fail("商品不存在");
        }
        SecurityUser user = SpringSecurityUtils.getUser();
        if (!user.getId().equals(goods.getOpId())){
            return RestResult.fail("您不能删除该商品");
        }
        goodsService.removeById(id);
        return RestResult.success();
    }

    public List<BaseGoodsDto> goodsBySupplier(Long supplierId) {

        List<BaseGoods> goodsList = goodsService.goodsBySupplier(supplierId);

        List<BaseGoodsDto> resultList = Lists.newArrayList();
        if (CollectionUtils.isEmpty(goodsList)){
            return resultList;
        }
        for (BaseGoods goods : goodsList) {
            BaseGoodsDto dto = Convert.convert(BaseGoodsDto.class, goods);
            resultList.add(dto);
        }
        return resultList;
    }
}
